<template>
  <div>
    <div>
      <div align="center" style="width: 100px">
        <div>
          <el-image
            style="width: 80px; height: 80px; border-radius: 40px;"
            fit="fill"
            :src="userIcon"
          ></el-image>
        </div>
        <div>
          <el-upload
            :on-success="onSuccess"
            :on-error="onError"
            :before-upload="beforeUpload"
            :action="iconUploadUrl"
            :with-credentials="true"
          >
            <el-button type="primary">上传头像</el-button>
          </el-upload>
        </div>
      </div>
      <br />

      <span class="tip">用户ID：</span>
      <span class="content">{{user.userId}}</span>
      <br />
      <br />

      <span class="tip">用户名：</span>
      <span class="content">{{user.userName}}</span>
      <br />
      <br />

      <span class="tip">手机号码：</span>
      <span class="content">{{user.phoneNumber}}</span>
      <br />
      <br />

      <span class="tip">用户类型：</span>
      <span class="content">{{user.userType}}</span>
      <br />
      <br />

      <span class="tip">用户积分：</span>
      <span class="content">{{user.points}}</span>
      <br />
      <br />

      <span class="tip">邮箱：</span>
      <span class="content">{{user.email}}</span>
      <el-button type="primary" style="margin-left: 10px;" @click="changeEmail()" size="mini">修改邮箱</el-button>
      <br />
      <br />

      <span class="tip">密码：</span>
      <el-button type="warning" @click="changePassword()" size="mini">修改密码</el-button>
      <br />
      <br />

      <!-- <el-button type="primary" @click="updateInfo()">保存信息</el-button> -->
    </div>

    <ChangePassword ref="changePassword" />
    <ChangeEmail ref="changeEmail" @updateEmail="updateEmail"/>
  </div>
</template>

<script>
import ChangePassword from "@/components/UserCenter/ChangePassword";
import ChangeEmail from "@/components/UserCenter/ChangeEmail";

export default {
  name: "Info",
  data() {
    return {
      userIcon: "",
      user: {},

      iconUploadUrl: process.env.URL_HEAD + "/touser/user/page/save/icon",

      rules: {
        email: [
          { pattern: /^(\w)+@(\S)+$/, message: "非法邮箱", trigger: "blur" },
        ],
        oldPassword: [{ required: true, message: "请输入", trigger: "blur" }],
        newPassword: [{ required: true, message: "请输入", trigger: "blur" }],
      },
    };
  },
  components: {
    ChangePassword,
    ChangeEmail,
  },
  methods: {
    init(user) {
      // console.log(user);
      this.user = user;
      this.userIcon = user.userIcon;
    },
    changePassword() {
      this.$refs.changePassword.init();
    },
    changeEmail() {
      this.$refs.changeEmail.init();
    },
    updateEmail(newEmail) {
      this.user.email = newEmail;
      this.$emit("userInfoUpdate", this.user);
    },
    beforeUpload(file) {
      var flag = true;

      if (file.size > 1024 * 1024 * 2) {
        this.$message.error("文件不能大于2MB");
        flag = false;
      }

      if (
        !(
          file.type === "image/jpeg" ||
          file.type === "image/png" ||
          file.type === "image/gif"
        )
      ) {
        this.$message.error("只接受：jpg/png/gif");
        flag = false;
      }

      // console.log(file);

      return flag;
    },
    onSuccess(res, file) {
      location.reload();
    },
    onError(res, file) {
      this.$message.error("上传失败");
    },
    sleep(numberMillis) {
      var now = new Date();
      var exitTime = now.getTime() + numberMillis;
      while (true) {
        now = new Date();
        if (now.getTime() > exitTime) return;
      }
    },
  },
  created: function () {},
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.el-input {
  width: 200px;
}

.tip {
  font-size: 16px;
}

.content {
  font-size: 16px;
}
</style>
